//
//  BLCertificateGradeVC.m
//  Byelone
//
//  Created by 乔小乔 on 2021/2/6.
//

#import "BLCertificateGradeVC.h"
#import "TZImagePickerController.h"

@interface BLCertificateGradeVC ()<UIActionSheetDelegate,TZImagePickerControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (unsafe_unretained, nonatomic) IBOutlet UIImageView *imgPic;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel *labelStatus;
@property (unsafe_unretained, nonatomic) IBOutlet UIButton *btnSubmit;
@property (nonatomic, strong) NSString *imgUrl;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel *labelTip;
@property (weak, nonatomic) IBOutlet UIButton *btnCamara;

@end

@implementation BLCertificateGradeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"学历认证";
    self.imgPic.layer.cornerRadius = 5;
    self.imgPic.layer.masksToBounds = YES;
    if (![self.dataModel.authStatus isEqualToString:@"0"]&&![NSString blIsBlankString:self.dataModel.authImg1]) {
        [self.imgPic sd_setImageWithURL:[NSURL URLWithString:self.dataModel.authImg1?:@""]];
        
        
        self.labelTip.hidden = YES;
        self.labelStatus.hidden = NO;
        self.labelStatus.text = [self getStatus:self.dataModel.authStatus];
        
        if (![self.dataModel.authStatus isEqualToString:@"3"]) {
            self.btnCamara.hidden = YES;
        }
    }
}
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.btnSubmit setGradientBackgroundWithColors:@[[UIColor chColorWithHexString:@"#8881FD"],[UIColor chColorWithHexString:@"#6B71F5"]] locations:@[@(0.0f), @(1.0f)]  startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];
}

- (NSString *)getStatus:(NSString *)status{
    NSDictionary *dict = @{@"0":@"待认证",@"1":@"认证中",@"2":@"认证通过",@"3":@"认证失败"};
    NSString *statusName = dict[status];
    return statusName;
}
- (IBAction)clickCamera:(id)sender {
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"上传学历" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"立即拍照",@"从本地相册上传", nil];
    [sheet showInView:self.view];
}

- (IBAction)clickSubmit:(id)sender {
    if ([self.dataModel.authStatus isEqualToString:@"1"]|| [self.dataModel.authStatus isEqualToString:@"2"]) {
        return;
    }
    NSDictionary *dict = @{@"gradeAuthImg":self.imgUrl?:@""};
    [BLHttpTool postWithPath:BL_USER_GRADEAUTH params:dict successBlock:^(NSDictionary * _Nonnull obj) {
        if (obj) {
            self.labelTip.hidden = YES;
            self.labelStatus.hidden = NO;
            self.labelStatus.text = obj[@"msg"];
            [[NSNotificationCenter defaultCenter]postNotificationName:Notification_login object:nil];
        }
    } failedBlock:^(NSURLSessionDataTask * _Nonnull data, NSError * _Nonnull error) {
        
    }];    
}
- (void)saveImage:(UIImage *)image{
    self.imgPic.image = image;
    [BLTool upLoadImageArray:@[image] type:UpLoadImageType_Avatar success:^(NSArray * _Nonnull imgUrlArray) {
        if (imgUrlArray.count > 0) {
            NSDictionary * photoDic = [imgUrlArray firstObject];
            NSString * imageUrl = [photoDic objectForKey:@"photoPath"];
            self.imgUrl = imageUrl;
        }
    } failure:^(NSString * _Nonnull errorString) {
        
    }];
}
#pragma mark - UIActionSheet Delegate

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.mediaTypes = @[(NSString*)kUTTypeImage];
        picker.delegate = self;
        [self presentViewController:picker animated:YES completion:nil];
    }else if (buttonIndex == 1){
        
        TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 columnNumber:4 delegate:self pushPhotoPickerVc:YES];
        imagePickerVc.isSelectOriginalPhoto = NO;
        
        imagePickerVc.allowTakePicture = NO; // 在内部显示拍照按钮
        imagePickerVc.allowPickingVideo = NO;
        imagePickerVc.allowPickingImage = YES;
        imagePickerVc.allowPickingOriginalPhoto = NO;
        imagePickerVc.allowPickingGif = NO;
        imagePickerVc.allowPickingMultipleVideo = NO; // 是否可以多选视频
        imagePickerVc.photoWidth = 3000;
        // 4. 照片排列按修改时间升序
        imagePickerVc.sortAscendingByModificationDate = NO;
        imagePickerVc.showSelectBtn = NO;
//        imagePickerVc.allowCrop = YES;
//        imagePickerVc.cropRect = CGRectMake(0, BL_WINDOW_HEIGHT/2-BL_WINDOW_WIDTH*0.5, BL_WINDOW_WIDTH, BL_WINDOW_WIDTH);
//        imagePickerVc.needCircleCrop = NO;
//        imagePickerVc.circleCropRadius = (BL_WINDOW_WIDTH-40)/2;
//        imagePickerVc.naviBgColor = [UIColor whiteColor];
//        imagePickerVc.barItemTextColor = [UIColor chColorWithHexString:@"#1B1C33"];
        
        [self presentViewController:imagePickerVc animated:YES completion:nil];
    }
}
#pragma mark - TZImagePickerControllerDelegate
- (void)tz_imagePickerControllerDidCancel:(TZImagePickerController *)picker {
}
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
    if (photos.count > 0) {
        UIImage *image = [photos firstObject];
        [self saveImage:image];
    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [self dismissViewControllerAnimated:YES completion:^{
        NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
        if ([mediaType isEqualToString:(NSString*)kUTTypeImage]){
            UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
            if (image) {
                [self saveImage:image];
            }
        }
    }];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
